﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LobbyPanel : MonoBehaviour
{
    public Button m_btnSet;
    public Button m_btnMail;
    public Button m_btnKF;
    public Button m_btnShop;
    public Button btn_share;
    public AndroidMgr androidmgr;
    
    //player
    public Image m_imgPlayerhead;
    public Text m_txtWin4;
    public Text m_txtFail4;
    public Text m_txtWin2;
    public Text m_txtFail2;
    public Text m_txtScoring;
    
    //entergame
    public Button m_btn1V4;
    public Button m_btn2V2;
    public Button m_btn1V4_quick;
    public Button m_btn2V2_quick;
    
    //paiming
    public Text m_txtGoldCoin;
    
    // Start is called before the first frame update
    void Start()
    {
        OnInitBtn();
        OninitPlayerInfo();
        EventCenter.AddListener<string>(EventType.On_UPDATE_GOLD, OnUpdateGoldCoin);
    }

    private void OnInitBtn()
    {
        m_btnSet.onClick.AddListener(() =>
        {
            Debug.Log("设置");
            AudioManager.Instance.OnPlayClick();
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)5);
        });
        m_btnMail.onClick.AddListener(() =>
        {
            Debug.Log("mail");
            AudioManager.Instance.OnPlayClick();
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)3);
        });
        m_btnKF.onClick.AddListener(() =>
        {
            Debug.Log("kf");
            AudioManager.Instance.OnPlayClick();
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)4);
        });
        btn_share.onClick.AddListener(() =>
        {
            Debug.Log("fx");
            AudioManager.Instance.OnPlayClick();
            androidmgr.OnShareToAndroid("https://play.google.com/store/apps/details?id=com.master.ludogame");
        });
        m_btnShop.onClick.AddListener(() =>
        {
            Debug.Log("shop");
            AudioManager.Instance.OnPlayClick();
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)6);
        });
        
        m_btn1V4.onClick.AddListener(() =>
        {
            Debug.Log("进入1v4游戏");
            AudioManager.Instance.OnPlayClick();
            LocalData.Instance.EnterGame = 1;
            LocalData.Instance.EnterGameType = 4;
            this.gameObject.SetActive(false);
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)7);
        });
        m_btn2V2.onClick.AddListener(() =>
        {
            Debug.Log("进入2v2游戏");
            AudioManager.Instance.OnPlayClick();
            LocalData.Instance.EnterGame = 2;
            LocalData.Instance.EnterGameType = 4;
            this.gameObject.SetActive(false);
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)7);
        });
        
        m_btn1V4_quick.onClick.AddListener(() =>
        {
            Debug.Log("进入1v4 快速 游戏");
            AudioManager.Instance.OnPlayClick();
            LocalData.Instance.EnterGame = 1;
            LocalData.Instance.EnterGameType = 1;
            this.gameObject.SetActive(false);
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)7);
        });
        
        m_btn2V2_quick.onClick.AddListener(() =>
        {
            Debug.Log("进入2v2 快速 游戏");
            AudioManager.Instance.OnPlayClick();
            LocalData.Instance.EnterGame = 2;
            LocalData.Instance.EnterGameType = 1;
            this.gameObject.SetActive(false);
            EventCenter.BroadCast(EventType.On_OPEN_PANEL,(object)7);
        });
    }

    private void OninitPlayerInfo()
    {
        string head = LocalData.Instance.OnSetPlayerHead();
        m_imgPlayerhead.sprite = Resources.Load<Sprite>("Image/Sprite/" + head);

        m_txtWin4.text = "0";
        m_txtFail4.text = "0";
        m_txtWin2.text = "0";
        m_txtFail2.text = "0";
        m_txtScoring.text = "0";

        int goldcoin = LocalData.Instance.OnGetPlayerGold();
        Debug.Log(""+goldcoin);
        m_txtGoldCoin.text = "" + goldcoin;
    }

    private void OnUpdateGoldCoin(string gold)
    {
        m_txtGoldCoin.text = gold;
        LocalData.Instance.OnSetPlayerGold(int.Parse(gold));
    }
    
    private void OnDestroy()
    {
        EventCenter.RemoveListener<string>(EventType.On_UPDATE_GOLD, OnUpdateGoldCoin);
    }
}
